/*==============================================================================
**                                 SEE4SYS
**                      Proprietary - Copyright (C) 2012
**------------------------------------------------------------------------------
**
** Stakeholder visibility: <PUBLIC>
** 
** Summary: See below.
** ____________________________________________________________________________
** ____________________________________________________________________________
** 
**= History ====================================================================
** Phuong.PHAM 2012-02-10
** - First creation & implementation
** Phuong.PHAM 2012-04-04
** - Fix PR#4685
** -----------------------------------------------------------------------------
==============================================================================*/
var Utils = new function Utils() {
	
    var instance = this;
    Utils.getInstance = function()
    {
            return instance;
    }

    /**
     * Log a message.
     */
    Utils.log = function() {
        if(typeof DEBUG == 'boolean' && DEBUG) {
            if(arguments.length > 1) {
                var type = arguments[0];
                //arguments.splice(0,1);
                if(type == LogType.ERROR) {
                    console.error(arguments);
                } else if(type == LogType.WARNING) {
                    console.warn(arguments);
                } else if(type == LogType.INFO) {
                    console.info(arguments);
                } else if(type == LogType.DEBUG) {
                    console.debug(arguments);
                } else {
                    console.log(type, arguments);
                }
            } else {
                console.log(arguments);
            }	
        }
    }

    /**
    * calculate MD5.
    * 
    * @param orgText
    * @returns a string that is encoded with MD5.
    */
    Utils.toMD5 = function(orgText) {
        return calcMD5(orgText);
    }
	
    /**
     * Check a test is empty or not.
     * @param text
     * @returns {Boolean} true if the text is empty otherwise false.
     */
    Utils.isEmpty = function(text) {
        if (text == null || text.length == 0) {
            return true;
        }
        return false;
    }
	
    /**
    * Check a test is not empty or empty.
    * @param text
    * @returns {Boolean} true if the text is not empty otherwise false.
    */
    Utils.isNotEmpty = function(text) {
        if (text == null || text.length == 0) {
            return false;
        }
        return true;
    }
	
    /**
     * To time string from total second. For example: 670 seconds -> 11:10.
     * 
     * @param totalSec the total of second.
     * @returns {String} a time string.
     */
    Utils.toTimeString = function(totalSec) {
        var hours = parseInt(totalSec / 3600) % 24;
        var minutes = parseInt(totalSec / 60) % 60;
        var seconds = totalSec % 60;

        var result = (hours > 0 ? (hours + ":") : "")
                    + (minutes < 10 ? "0" + minutes : minutes) + ":"
                    + (seconds < 10 ? "0" + seconds : seconds);
        return result;
    }
	
    /**
    * Show a confirm dialog.
    * 
    * @param title the title of dialog.
    * @param message the message of dialog.
    * @param callback the callback will be executed if user press YES.
    */
    Utils.showConfirmDialog = function(title, message, callback, cancelCallback) {
        $('<div>').simpledialog2({
            mode : 'button',
            headerText : title,
            headerClose : true,
            buttonPrompt : message,
            width : '350px',
            buttons : {
                    'OK' : {
                        click : function() {
                        $('#buttonoutput').text('OK');
                            if (callback != null) {
                                callback();
                            }
                        }
                    },
                    'Cancel' : {
                        click : function() {
                            $('#buttonoutput').text('Cancel');
                            if (cancelCallback != null) {
                                    cancelCallback();
                            }
                        },
                        icon : "delete",
                        theme : "c"
                    }
                }
        });
    }

    Utils.countJsonElement = function (json) {
        var count = 0;
        $.each(json, function() {count++;});
        
        return count;
    }
    
    return Utils;
}

///////////////////////////////////////////
//   Select util                         //
///////////////////////////////////////////
var SelectUtils = new function SelectUtils() {
	
    var instance = this;
    SelectUtils.getInstance = function()
    {
            return instance;
    }
    
    /**
     * Insert an option before selected one
     * selecteEle: id of select DOM
     * value: option's value
     * text: option's text
     */
    SelectUtils.insertOptionBefore = function(selectEle, value, text) {
        var elSel = document.getElementById(selectEle);
        if (elSel.selectedIndex >= 0) {
            var elOptNew = document.createElement('option');
            elOptNew.text = text;
            elOptNew.value = value;
            var elOptOld = elSel.options[elSel.selectedIndex];  
            try {
                elSel.add(elOptNew, elOptOld); // standards compliant; doesn't work in IE
            }
            catch(ex) {
                elSel.add(elOptNew, elSel.selectedIndex); // IE only
            }
        }
    }

    /**
     * Remove selected element
     * selecteEle: id of select DOM
     */
    SelectUtils.removeOptionSelected = function(selectEle) {
        var elSel = document.getElementById(selectEle);
        var i;
        for (i = elSel.length-1; i >= 0; i--) {
            if (elSel.options[i].selected) {
                elSel.remove(i);
            }
        }
    }

    
    SelectUtils.appendOptionLast = function(selectEle, value, text) {
        var elOptNew = document.createElement('option');
        elOptNew.text = text;
        elOptNew.value = value;
        var elSel = document.getElementById(selectEle);

        try {
            elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
        }
        catch(ex) {
            elSel.add(elOptNew); // IE only
        }
    }

    /*
     * Remove last option
     */
    SelectUtils.removeOptionLast = function(selectEle) {
        var elSel = document.getElementById(selectEle);
        if (elSel.length > 0) {
            elSel.remove(elSel.length - 1);
        }
    }
    
    SelectUtils.clearOptions = function(selectEle) {
	document.getElementById(selectEle).options.length = 0;
    }
    
    return SelectUtils;
}